/**
 * 
 */
package com.zc.BankCreditManagement.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.zc.BankCreditManagement.BasicDAO.BasicDAO;
import com.zc.BankCreditManagement.Exception.ZCException;
import com.zc.BankCreditManagement.JavaBean.CustomerCreditLevel;

/**
 * @author ZhangCheng
 * @time 2019年9月17日
 */
public class CustomerCreditLevelDAO extends BasicDAO<CustomerCreditLevel> {
	/**
	 * CustomerCreditLevelDAO无参构造 重载父类构造器
	 */
	public CustomerCreditLevelDAO() {
		super();
	}

	/**
	 * CustomerCreditLevelDAO有参构造 重载父类构造器
	 */
	public CustomerCreditLevelDAO(String url, String user, String pwd) {
		super(url, user, pwd);
	}

	/**
	 * getCustomerCreditLevelByCusId(String cusId) 通过用户Id获取该用户目前的信用等级表
	 * 
	 * @param cusId 用户id excludeDelete 是否排除已经被标记删除的
	 */
	public CustomerCreditLevel getCusCreditLevelByCusId(int cusId, boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from CustomerCreditLevel where cusid = ? ");
		if (excludeDelete)
			sql.append("and isdelete = 0");// 如果排除被删除的，那么只返回未被删除的
		ArrayList<CustomerCreditLevel> cCL = getInfoFromDB(sql.toString(), cusId);
		if (cCL.size() == 0) {
			return null;
		} else {
			return cCL.get(0);
		}
	}

	/**
	 * deleteOldCusCreditLevelById(String cusId) 删除旧的信用等级
	 * 
	 * @param cusId 用户id excludeDelete 是否排除已经被标记删除的
	 */
	public int deleteOldCusCreditLevelById(int cusId,boolean excludeDelete) throws ZCException {
		int count = 0;
		if (getCusCreditLevelByCusId(cusId, excludeDelete) == null) {// 已经被删除或者未及时更新isdelete
			throw new ZCException("没有该客户的当前有效信用等级或该客户上期信用等级未及时更新！");
		} else {
			String sql = "update CustomerCreditLevel set isdelete = 1 where cusid = ?";
			try {
				count = updateDML(sql, cusId);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return count;
	}

	/**
	 * deleteOldCusCreditLevelByIdFromDB(String cusId) 删除旧的信用等级
	 * 
	 * @param cusId 用户id excludeDelete 是否排除已经被标记删除的
	 */
	public int deleteOldCusCreditLevelByIdFromDB(int cusId,boolean excludeDelete) throws ZCException {
		int count = 0;
		if (getCusCreditLevelByCusId(cusId, excludeDelete) == null) {// 已经被删除或者未及时更新isdelete
			throw new ZCException("没有该客户的当前有效信用等级或该客户上期信用等级未及时更新！");
		} else {
			String sql = "delete from CustomerCreditLevel where cusid = ?";
			try {
				count = updateDML(sql, cusId);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return count;
	}

	/**
	 * insertNewCustomerCreditLevel(CustomerCreditLevel cusCL) 插入新的当前日期范围内的信用等级记录
	 */
	public int insertNewCusCreditLevel(CustomerCreditLevel cusCL) {
		int count = 0;
		String sql = "Insert into CustomerCreditLevel values(?,?,?,?,?,?)";
		try {
			count = super.updateDML(sql, cusCL.getCreditlevelId(), cusCL.getCusId(), cusCL.getCreditLevel(), cusCL.getThisLevelBeginDate(),
					cusCL.getThisLevelEndDate(), 0);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public ArrayList<CustomerCreditLevel> resultSetToArrayList(ResultSet re) {
		ArrayList<CustomerCreditLevel> cusCL = new ArrayList<CustomerCreditLevel>();

		try {
			while (re.next()) {
				cusCL.add(new CustomerCreditLevel(re.getString("creditlevelId"),re.getInt("cusid"), re.getInt("creditLevel"),
						re.getString("thisLevelBeginDate"), re.getString("thisLevelEndDate")));
//				System.out.println(c.get(0).getInfo());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.closeAll();
		return cusCL;
	}

}
